/*
 *  Copyright (c) 2013 Hai Bison
 *
 *  See the file LICENSE at the root directory of this project for copying
 *  permission.
 */

package com.haibison.android.hutieu;

import java.math.BigInteger;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;

import com.haibison.android.hutieu.utils.SimpleWeakEncryption;

/**
 * System utilities.
 * 
 * @author Hai Bison
 * 
 */
public class Sys {

    /**
     * The UUID of this library. This was generated by Python 3 ^^
     */
    public static final String UUID = "2412b62e-466e-11e3-8154-001fd0ca4f8c";

    /**
     * The library name.
     */
    public static final String LIB_NAME = "Hủ tiếu";

    /**
     * The library "code name".
     */
    public static final String LIB_CODE_NAME = "hu-tieu";

    /**
     * The library version name.
     */
    public static final String LIB_VERSION_NAME = "1.5.1";

    /**
     * The library version code.
     */
    public static final int LIB_VERSION_CODE = 12;

    /**
     * This is singleton class.
     */
    private Sys() {
    }// Sys()

    /**
     * Prefixes given name with {@link #LIB_CODE_NAME} and {@link #UUID}. The
     * purpose is to generate unique names used for this library only, such as
     * preferences file name...
     * 
     * @param name
     *            the name.
     * @return the prefixed name.
     */
    public static String prefix(String name) {
        return LIB_CODE_NAME + '.' + UUID + '.' + name;
    }// prefix()

    /**
     * Gets the <b>unique</b> signature which belongs to this app.
     * 
     * @param context
     *            the context.
     * @return the unique signature.
     */
    public static byte[] getSignature(Context context) {
        final PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), PackageManager.GET_SIGNATURES);
        } catch (NameNotFoundException e) {
            /*
             * We should never be here. But in that case, return this one.
             */
            return SimpleWeakEncryption.sha256(context.getPackageName());
        }

        Signature[] signatures = packageInfo.signatures;
        if (signatures == null || signatures.length <= 0)
            return SimpleWeakEncryption.sha256(context.getPackageName());

        /*
         * We should reach this.
         */
        byte[] result = SimpleWeakEncryption
                .sha256(signatures[0].toByteArray());
        for (int i = 0; i < Byte.MAX_VALUE; i++)
            result = SimpleWeakEncryption.sha256(result);
        return result;
    }// getSignature()

    /**
     * Gets the <b>unique</b> signature which belongs to this app.
     * 
     * @param context
     *            the context.
     * @return the unique signature.
     */
    public static String getSignatureAsString(Context context) {
        return new BigInteger(getSignature(context))
                .toString(Character.MAX_RADIX);
    }// getSignatureAsString()

    /**
     * Gets the <b>unique</b> signature which belongs to this app.
     * 
     * @param context
     *            the context.
     * @return the unique signature.
     */
    public static char[] getSignatureAsChars(Context context) {
        return getSignatureAsString(context).toCharArray();
    }// getSignatureAsChars()

}
